//
//  OriginalView.swift
//  SinaBlog
//
//  Created by macs on 15/11/14.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
import FFLabel

let IconViewCornRaius : CGFloat = 17
let verifiedViewOffset = -4
//原创微博视图
class OriginalView: UIView {
    
    var bottomConstrain : Constraint?
    var status : Status? {
        
        didSet {
            
            iconView.sd_setImageWithURL(status?.user?.headImageUrl, placeholderImage: UIImage(named: "avatar_default_big"))
            
            nameLable.text = status?.user?.name
            brankView.image = status?.user?.mbrankImage
            verifiedView.image = status?.user?.verifiedImage
//            createdLable.text = "11:11" //status?.created_at
            createdLable.text = status?.created_date
            //print(status?.source)
//            sourceLable.text = "wwww" //status?.source
            sourceLable.text = status?.sourceRegul
            //对微博内容进行处理,显示属性文本
            contentLable.attributedText = EmojiManage.shareManage.getAttributeText(status?.text ?? "", font: contentLable.font)
            
            
            //在设置配图之前需要清空约束类型.然后再更新约束
            bottomConstrain?.uninstall()
            if let puls = status?.pictureUrl where puls.count > 0 {
                //有配图视图
                pictureView.imageUrls = status?.pictureUrl
                //为了解决 cell复用,显示配图视图
                pictureView.hidden = false
                //约束的类型更新
                snp_updateConstraints(closure: { (make) -> Void in
                   self.bottomConstrain = make.bottom.equalTo(pictureView.snp_bottom).offset(statuscellMargin).constraint
                    })
                
            } else {
          
                //为了解决 cell复用,隐藏配图视图
                pictureView.hidden =  true
                //没有配置视图
                snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstrain = make.bottom.equalTo(contentLable.snp_bottom).offset(statuscellMargin).constraint
                })
                
            }
            
        }
    }
    
    //3.重写构造方法
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        //设置 contentlable 的代理
        contentLable.labelDelegate = self
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //2.添加子控件,设置约束
    func setupUI() {
        sepView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        addSubview(sepView)
        addSubview(iconView)
        addSubview(nameLable)
        addSubview(brankView)
        addSubview(verifiedView)
        addSubview(createdLable)
        addSubview(sourceLable)
        addSubview(contentLable)
        addSubview(pictureView)
        
        sepView.snp_makeConstraints { (make) -> Void in
           make.top.left.width.equalTo(self)
            make.height.equalTo(statuscellMargin)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(statuscellMargin)
            make.left.equalTo(snp_left).offset(statuscellMargin)
            make.width.equalTo(statusiconWidth)
            make.height.equalTo(statusiconWidth)
        }
        
        nameLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(statuscellMargin)
        }
        
        brankView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(nameLable.snp_right).offset(statuscellMargin)
        }
        
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right).offset(verifiedViewOffset)
            make.centerY.equalTo(iconView.snp_bottom).offset(verifiedViewOffset)
        }
        
        createdLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(statuscellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(createdLable.snp_right).offset(statuscellMargin)
            make.bottom.equalTo(createdLable)
        }
        
        contentLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_left)
            make.top.equalTo(iconView.snp_bottom).offset(statuscellMargin)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLable.snp_bottom).offset(statuscellMargin)
            make.left.equalTo(contentLable.snp_left)
//                        make.width.equalTo(100)
//                        make.height.equalTo(100)
        }
        
        snp_makeConstraints { (make) -> Void in
            
            //这里需要将约束条件转为约束类型
            self.bottomConstrain = make.bottom.equalTo(pictureView.snp_bottom).offset(statuscellMargin).constraint
        }
        
    }
    //1.懒加载控件
    //分割段
    lazy var sepView : UIView = UIView()
    //头像
    lazy var iconView : UIImageView = {
        let image = UIImage(named: "avatar_default_big")
        let imageView = UIImageView(image: image)
        
        imageView.layer.cornerRadius = IconViewCornRaius
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    //称号
    lazy var nameLable : UILabel = UILabel(title: "lable", size: 14, color: UIColor.orangeColor())
    //vip 等级y 
    lazy var brankView : UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //认证等级
    lazy var verifiedView : UIImageView = UIImageView(image: UIImage(named:"avatar_vip"))
    //发布时间
    lazy var createdLable : UILabel = UILabel(title: "11;11", size: 10, color: UIColor.lightGrayColor())
    //发布来源
    lazy var sourceLable : UILabel = UILabel(title: "www", size: 10, color: UIColor.lightGrayColor())
    //微博正文
    //lable的preferredMaxLayoutWidth属性决定了其最大宽度
    lazy var contentLable : FFLabel = FFLabel(title : "UIImageView = UIImageView(image: UIUIImageView = UIImageView(image: UI" , size : 14 ,color : UIColor.darkGrayColor() ,margin : statuscellMargin)
    lazy var pictureView : PictureView = PictureView()
    
}

extension OriginalView : FFLabelDelegate {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        if text.hasPrefix("http") {
            
            let webView = HomeWebViewController()
            webView.urlString = text
            self.getNavgationController()?.pushViewController(webView, animated: true)
        }
        
    }
    
}
